<?php


namespace App\Service\api;


use App\Common\RedisUtils;
use App\Common\Res;
use App\Common\SendMsgUtils;
use App\Common\StringUtils;
use App\Constants\ErrorCode;
use App\Model\form\AttachmentTrait;
use App\Model\form\WeworkAccountTrait;
use App\Model\WeworkFriend;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Hyperf\Config\Annotation\Value;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\Utils\Context;

class WeworkFriendService
{
    use StringUtils, Res, RedisUtils,
        SendMsgUtils, WeworkAccountTrait, AttachmentTrait;

    /**
     * @Value("commonset.hostUrl")
     */
    protected $hostUrl;

    /**
     * 聊天页获取客服列表
     * @param array $params
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getFriendsInfo(array $params): array
    {
        $data['data'] = [];
        try {
            $where = function ($query) use ($params) {
                $query->where('userId', $params['friendsId']);
            };
            $friendInfo = WeworkFriend::where($where)->first();
            if (!empty($friendInfo)) {
                $data['data']['userNickName'] = $friendInfo['nickname'];
                $data['data']['realName'] = $friendInfo['bookname'];
                $data['data']['userId'] = $friendInfo['userId'];
                $data['data']['avatarUrl'] = $this->hostUrl . $friendInfo['path']; // 头像
                $data['data']['remark'] = $friendInfo['remark'];
                $data['data']['sex'] = $friendInfo['sex']; // 性别 2为女 1为男
                $data['data']['marketUserId'] = $this->getWeworkAccountMarketUserId(['id'=>$friendInfo['account_id']]); // 账号编号
                $data['data']['corpTagList'] = [];
            }
            $data['code'] = ErrorCode::SUCCESS;
            $data['msg'] = '操作成功';
            return $data;
        } catch (\Throwable $ex) {
            $data['code'] = ErrorCode::ERROR;
            $data['msg'] = '操作失败';
            return $data;
        }
    }


    /**
     * 获取好友备注
     * @param array $params
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function setRemark(array $params): array
    {
        try {
            $where = function ($query) use ($params) {
                $query->where('userId', $params['friendsId']);
            };
            $friendInfo = WeworkFriend::where($where)->first();
            if (!empty($friendInfo)) {
                $friendInfo->remark = $params['remark'];
                $result = $friendInfo->save();
                $data['code'] = ErrorCode::SUCCESS;
                $data['msg'] = '修改备注成功';
            } else {
                $data['code'] = ErrorCode::ERROR;
                $data['msg'] = '修改备注失败';
            }
            return $data;
        } catch (\Throwable $ex) {
            $data['code'] = ErrorCode::ERROR;
            $data['msg'] = '修改备注失败';
            return $data;
        }
    }


}